package com.tarena.luban.demo.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Supplier;

@Component
public class Myfilter03 implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 拿到请求对象
        ServerHttpRequest request = exchange.getRequest();
        // 请求对象相关数据
        URI uri = request.getURI();
        RequestPath path = request.getPath();
        HttpMethod method = request.getMethod();
        System.out.println("请求uri为"+uri);
        System.out.println("请求path为"+path);
        System.out.println("请求method为"+method);

        // 拿到响应对象
        ServerHttpResponse response = exchange.getResponse();
        // 得到响应头
        HttpHeaders headers = response.getHeaders();
        // 注意乱码格式
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 请求参数
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        System.out.println(queryParams);
        if (queryParams.size()==0){

            // 准备一个输出字符串
            String resultJson = "{\"code\":500,\"message\":\"您的请求参数为空，解析失败！\"}";
            // 5. 将字符串包装到响应中 从这个过滤器输出响应
            // 5.1 从响应中拿到转化的一个数据缓存对象 用这个对象包装一些字符串 二进制
            DataBufferFactory dataBufferFactory = response.bufferFactory();
            // 5.2 使用缓存数据流对象 将字符串 为禁止包装
            DataBuffer wrap = dataBufferFactory.wrap(resultJson.getBytes(StandardCharsets.UTF_8));
            // 创建一个mono对象 返回
            Mono<DataBuffer> writeData = Mono.fromSupplier(new Supplier<DataBuffer>() {
                @Override
                public DataBuffer get() {
                    return wrap;
                }
            });
            return response.writeWith(writeData);
        }
        List<String> name = queryParams.get("name");
        if(name.get(0)==null){
            // 准备一个输出字符串
            String resultJson = "{\"code\":500,\"message\":\"您的请求参数值为空 解析失败！\"}";
            DataBufferFactory dataBufferFactory = response.bufferFactory();
            DataBuffer wrap = dataBufferFactory.wrap(resultJson.getBytes(StandardCharsets.UTF_8));
            Mono<DataBuffer> writeData = Mono.fromSupplier(new Supplier<DataBuffer>() {
                @Override
                public DataBuffer get() {
                    return wrap;
                }
            });
            return response.writeWith(writeData);
        }
        Mono<Void> filter = chain.filter(exchange);
        return filter;
    }
}
